package com.base.control;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.model.ContextDefinition;


/**
 * control view jump
 * @author yjh
 *
 */
public class ContextControl {

    public static void bindClickStartAcitivity(final Context context, final  Class<? extends Activity> claz, View source, final boolean finishSource) {
        source.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startContext(context, claz, finishSource);
            }
        });
    }

    public static void bindClickStartAcitivity(final Context context, final  Class<? extends Activity> claz, View source, final boolean finishSource, final Bundle data) {
        source.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startContext(context, claz, finishSource, data);
            }
        });
    }

	public static void startContext(Context context, Class<? extends Activity> claz, Bundle data) {
		if(context != null && claz != null) {
			Intent intent = new Intent();
			intent.setClass(context, claz);
            if(data != null) {
                intent.putExtras(data);
            }
			context.startActivity(intent);
		}
	}

    public static void startContextForResultFromFragment(MFragmentSupport context, Class<? extends Activity> claz, int requestCode, boolean finishSource, Bundle data) {
        if(context != null) {
            Intent intent = new Intent();
            intent.setClass(context.getMactivity(), claz);
            if(data != null) {
                intent.putExtras(data);
            }
            context.startActivityForResult(intent, requestCode);
        } else {
            throw new NullPointerException();
        }

        if(finishSource && isActivity(context.getClass())) {
            context.getMactivity().finish();
        }
    }

    public static void startContext(Context context, Class<? extends Activity> claz) {
        startContext(context, claz, null);
    }

    /**
     *
     * @param context
     * @param claz
     * @param finishSource 是否将当前activity退出栈
     */
    public static void startContext(Context context, Class<? extends Activity> claz, boolean finishSource) {
        startContext(context, claz, finishSource, null);
    }

    public static void startContext(Context context, Class<? extends Activity> claz, boolean finishSource, Bundle data) {
        startContext(context, claz, data);
        if(finishSource && isActivity(context.getClass())) {
            ((Activity)context).finish();
        }

    }
	
	//using intent start activity, service and so on
	@SuppressWarnings("unchecked")
	public static <T> void startContext(ContextDefinition<T> definition, Context context, Bundle data) {
		//notice Activity.class.getClass()
		if(isActivity(definition.getViewClass())) {
			startContext(context, (Class<Activity>)definition.getViewClass(), data);
		}
	}

    public static <T> void startContext(View clickable, final ContextDefinition<T> definition, final Context context, final Bundle data) {
        clickable.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                startContext(definition, context, data);
            }
        });
    }

    public static boolean isActivity(Class claz) {
        return Activity.class.getClass().isInstance(claz);
    }
}
